/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-30     yoyoyes      first version
 */

#include "timer_dynamic_demo.h"

static rt_timer_t timer1;           /* 定时器1控制块 */
static rt_timer_t timer2;           /* 定时器2控制块 */

static int cnt = 0;

/*
 * @brief  定时器1超时函数
 *
 * @param  parameter 定时器超时函数参数
 *
 * @return None
 */
static void timeout1(void *parameter)
{
    rt_kprintf("periodic timer is timeout %d\n", cnt);

    if(cnt++ >= 9)
    {
        rt_timer_stop(timer1);
        rt_kprintf("periodic timer was stopped!\n");
    }
}

/*
 * @brief  定时器2超时函数
 *
 * @param  parameter 定时器超时函数参数
 *
 * @return None
 */
static void timeout2(void *parameter)
{
    rt_kprintf("one shot timer is timeout\n");
}

/*
 * @brief  定时器Demo函数
 *
 * @param  None
 *
 * @return None
 */
void timer_dynamic_demo(void)
{
    /* 创建周期定时器 */
    timer1 = rt_timer_create("timer1", timeout1, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC);
    if(timer1 != RT_NULL)
    {
        rt_timer_start(timer1);
    }

    /* 创建单次定时器 */
    timer2 = rt_timer_create("timer2", timeout2, RT_NULL, 30, RT_TIMER_FLAG_ONE_SHOT);
    if(timer2 != RT_NULL)
    {
        rt_timer_start(timer2);
    }
}

